import sys
from bs4 import BeautifulSoup
import json
from util import py_dl
from util import py_socket


def parse_url(url, is_file=False):
    # detail = {}
    detail = dict()
    detail['url'] = url
    if is_file:
        soup = BeautifulSoup(open(url, encoding='utf-8'), 'lxml')
    else:
        html_doc = py_dl.dl_url(url)
        soup = BeautifulSoup(html_doc, 'lxml')
    # detail['title'] = soup.title.string
    detail['label'] = soup.find(class_='panel-title').string

    dl_url= soup.find(class_='card-footer-item').get('href')
    dl_url = dl_url[0:60]
    detail['detail'] = dl_url
    py_socket.send_msg(json.dumps(detail))
    # print(json.dumps(detail))


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print('缺少参数！')
    else:
        input_url = sys.argv[1]
        if input_url.startswith('http'):
            parse_url(input_url)
        else:
            parse_url(input_url, True)